﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyTiemVang.DAO
{
    class PhieuNhapKhoDAO
    {
        private static QUANLYTIEMVANGEntities db = new QUANLYTIEMVANGEntities();

        public static List<PHIEUNHAPKHO> GetAllPhieuMuaHang()
        {
            return db.PHIEUNHAPKHOes.ToList();
        }

        public static void Insert(PHIEUNHAPKHO pnk)
        {
            List<CHITIETPHIEUNHAPKHO> list = pnk.CHITIETPHIEUNHAPKHOes.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                CHITIETPHIEUNHAPKHO ct = list[i];
                ct.ThanhTien = ct.SoLuong * ct.DonGia;
            }
            db.PHIEUNHAPKHOes.Add(pnk);
            db.SaveChanges();
        }
        public static void Delete(PHIEUNHAPKHO pnk)
        {
            var query = db.PHIEUNHAPKHOes.Find(pnk.MaPhieuNhapKho);
            if (query != null)
            {
                db.PHIEUNHAPKHOes.Remove(query);
                db.SaveChanges();
            }
        }

        public static List<PHIEUNHAPKHO> GetAllPhieuNhapKho()
        {
            return db.PHIEUNHAPKHOes.ToList();
        }
        public static int GetLastMaPhieuNhapKho()
        {
            var query = (from d in db.PHIEUNHAPKHOes
                         select d.MaPhieuNhapKho).ToList();

            if (query.Count == 0)
            {
                return 1;
            }

            return query.Max() + 1;
        }
    }
}
